package cn.csxw.openchina.ui.activity;

import android.hardware.Sensor;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.view.View;

import cn.csxw.openchina.R;
import cn.csxw.openchina.base.BaseActivity;
import cn.csxw.openchina.utils.ShakeUtil;

/**
 * 摇一摇界面
 */
public class ShakeActivity extends BaseActivity {

    private ShakeUtil mShakeUtil;
    private SensorManager mSensorManager;

    @Override
    public int getLayoutRes() {
        return R.layout.activity_shake;
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public void initView() {
        //获取工具类
        mShakeUtil = new ShakeUtil(this);
    }

    @Override
    protected void onPause() {
        mSensorManager.unregisterListener(mShakeUtil);
        //回退时闪一下
        this.overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
        super.onPause();
    }
    @Override
    protected void onResume() {
        super.onResume();
        //获取传感器管理服务
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        //注册加速度传感器
        // SensorEventListener listener, Sensor sensor,
        //int samplingPeriodUs, int maxReportLatencyUs
        mSensorManager.registerListener(mShakeUtil,
                mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                //  指定了默认的数据延迟SensorManager.SENSOR_DELAY_NORMAL
                //还有SENSOR_DELAY_UI、SENSOR_DELAY_FASTEST、SENSOR_DELAY_GAME等，
                //根据不同应用，需要的反应速率不同，具体根据实际情况设定
                SensorManager.SENSOR_DELAY_NORMAL);
    }
    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View v, int id) {

    }
}
